
#include "iic.h"
#include "vl53lxx.h"
#include "vl53l1_api.h"
#include "general_gpio.h"

u16 vl53lxxId = 0;	/*vl53оƬID*/
bool isEnableVl53lxx = true;		
bool isInitvl53l1x = false;	
bool reInitvl53l1x = false;	

static u8 validCnt = 0;
static u8 inValidCnt = 0;

static u16 range_last = 0;
float quality = 1.0f;

zRange_t vl53lxx;

/**
 * @Description 初始化激光模块
 */	
void IICtoLaser_Init(void)
{
	SoftWare_IIC_Init();	
	delay_ms(10);
	
	//读取模块id
	VL53L1_RdWord(&dev, 0x010F, &vl53lxxId);
	
	if(vl53lxxId == VL53L1X_ID)	{
		//模块正常、可以起飞
		reInitvl53l1x = true;
		return;
	}
	else {	
		//模块初始化失败、不能起飞	
		while(1) {	
			BEEP_ON;
			delay_ms(5);
			BEEP_OFF;
			delay_ms(5);
		}
	}
}


/**
 * @Description 读取激光测距的值
 */	
float vl53l1xMeasure(void)
{
	int status;
	u8 isDataReady = 0;
	static VL53L1_RangingMeasurementData_t rangingData;
	
	//查询数据就绪
	status = VL53L1_GetMeasurementDataReady(&dev, &isDataReady);
				
	if(isDataReady)
	{
		//读取数据
		status = VL53L1_GetRangingMeasurementData(&dev, &rangingData);
		if(status==0)
		{
			//数据单位是mm，需要转换成米
			range_last = rangingData.RangeMilliMeter * 0.1f;	
			/*RT_Info.LASER_Alt = range_last/100.0f;*/
			return range_last/100.0f;				
		}
		status = VL53L1_ClearInterruptAndStartMeasurement(&dev);
	}		
	return 0;
}






